#include <stdio.h>
#include <malloc.h>
#include <assert.h>

#include "Terrain.h"

void terInit(Terrain *pTer)
{
	int x,y;
	
	const char terrain_defaut[16][16] = {
		"xxx.............",
		"x...............",
		"xxx.............",
		"x...########....",
		"xxx#..#*..#.....",
		"...#..*.*.###...",
		"....*.#...*.#...",
		"...####*##*.#...",
		"...#........#...",
		"...#.#####*.#...",
		"...#.*..*.*.#...",
		"...#.*......#...",
		"...#######..#...",
		".........####...",
		"................"
		};

	pTer->dimX = 16;
	pTer->dimY = 16;
	
	pTer->tab = (char **)malloc(sizeof(char *)*pTer->dimY);
	for (y=0; y<pTer->dimY; y++)
		pTer->tab[y] = (char *)malloc(sizeof(char)*pTer->dimX);

	for(y=0;y<pTer->dimY;++y)
		for(x=0;x<pTer->dimX;++x)
			pTer->tab[y][x] = terrain_defaut[y][x];
}

void terLibere(Terrain *pTer)
{
	int y;
	
	for (y=0; y<pTer->dimY; y++)
		free(pTer->tab[y]);
	free(pTer->tab);
	
	pTer->dimX = 0;
	pTer->dimY = 0;
	pTer->tab = NULL;
}

int  terEstPositionSokoValide(const Terrain * pTer, const int x, const int y)
{
	if (x>=0 && x<pTer->dimX && y>=0 && y<pTer->dimY && pTer->tab[y][x]!='#')
		return 1;
	else
		return 0;
}

int terEstPositionCaisseValide(const Terrain *pTer, const int x, const int y)
{
	if (x>=0 && x<pTer->dimX && y>=0 && y<pTer->dimY && pTer->tab[y][x]!='#' )
		return 1;
	else
		return 0;
}


const char terGetXY(const Terrain *pTer, const int x, const int y)
{
	assert( x>=0)  ;
	assert( y>=0) ;
	assert( x<pTer->dimX ) ;
	assert( y<pTer->dimY ) ;
	return pTer->tab[y][x];
}

void terSetXY(const Terrain *pTer, const int x, const int y, const char val)
{
	assert( x>=0) ;
	assert( y>=0) ;
	assert( x<pTer->dimX ) ;
	assert( y<pTer->dimY ) ;
	pTer->tab[y][x] = val;
}

const int getDimX(const Terrain *pTer)
{
	return pTer->dimX;
}

const int getDimY(const Terrain *pTer)
{
	return pTer->dimY;
}
